import services from "./services";
import Axios from 'axios'
export default {
  nuxtServerInit ({ commit }, { req }) {
    if (req.session && req.session.user) {
      commit('SET_USER', req.session.user)
    }
  },
  getWechatSignature({ commit }, url) {
    return services.getWechatSignature(url)
  },
  async getProducts({ commit }) {
    let res = await Axios.get('/shop/products')
    return res.data
  },
  async getProduct({ commit }, _id) {
    let res = await Axios.get(`/shop/product?_id=${_id}`)
    return res.data
  },
  async addProduct({ commit }, product) {
    let res = await Axios.post('/shop/add', product)
    return res.data
  },
  async updateProduct({ commit }, product) {
    let res = await Axios.post('/shop/update', product)
    return res.data
  },
  async delProduct({ commit }, _id) {
    let res = await Axios.post('/shop/del', { _id })
    return res.data
  },
  async login({ commit }, { email, password }) {
    let res = await Axios.post('/admin/loging', { email, password })
    if (res.data.success) commit('SET_USER', res.data.data)
    return res.data
  },

  async uplaod({ commit }, data) {
    console.log(`dataconsole.log(data)`)
    console.log(data)
    let config = {
      headers:{'Content-Type':'multipart/form-data'}
    }
    let res = await Axios.post('/shop/upload', data, config)
    return res.data
  }
}
